﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Sale
{
    public partial class 出库单查询 : System.Web.UI.Page
    {
        protected string Notice = "";
        string sql = @"
SELECT
订货单位ID,
cp.[品名]+' '+cp.[钢级]+' '+cp.[规格] as 品名,
发往单位,
订货单位ID,
sum(整车捆数) as 整车捆数,
sum(整车吨位) as 整车吨位,
sum(整车支数) as 整车支数,
sum(整车米数) as 整车米数,
发运时间,
是否合格
from [产品出库清单] as ck inner join
[产品] as cp ON ck.产品类别ID=cp.ID";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.DingHuo.DataBind();               
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.GridView1.SelectedIndex = -1;
          

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Message.Text = "·准备就绪";
        }
        private string GetState()
        {
            string state = " where ck.[是否合格]=1";

            //给一个默认值1
            if (this.ChanPin.Text != "")
            {
                state += " and ck.[产品类别ID] =" + this.ChanPin.Text;
            }
            else {

                state += " and ck.[产品类别ID] = 1"; 
                    
            }

            if (this.DingHuo.Text != "0")
            {
                state += " and ck.[订货单位ID] =" + this.DingHuo.Text;
            }          

            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and ck.[发运时间]>='" + d.ToShortDateString() + "'";
            }
            DateTime d1;
            if (DateTime.TryParse(this.Date2.Text, out d1))
            {
                state += " and ck.[发运时间]<='" + d1.ToShortDateString() + "'";
            }

            //if (state.StartsWith(" and"))
            //{
            //    state = state.Substring(4);
            //}
         
            

            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {            
            string state = this.GetState();
            this.SqlText.Text = sql + state + " group by 产品类别ID,cp.[品名],cp.[钢级],cp.[规格],发往单位,订货单位ID,发运时间,是否合格 order by 发运时间 desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            this.Message.Text = "·准备就绪";
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.ChanPin.Text = "0";            
            this.DingHuo.Text = "0";          
            this.Date1.Text = "";                  
        }

        protected void Look_Click(object sender, EventArgs e)
        {            
            Button_Query_Click(null, null);
            int selectRow = this.GridView1.SelectedIndex;
            if (selectRow == -1)
            {
                this.Message.Text = "·请选择一条记录";
                return;
            }
            else
            {
                this.Message.Text = "·准备就绪";
            }
            string chanpin=((Label)this.GridView1.Rows[selectRow].Cells[0].FindControl("Label3")).Text;  
            string CID = this.ChanPin.Text;
            if (chanpin == "油管(J55)")
            {
                CID = "1";
            }
            if (chanpin == "套管(J55)")
            {
                CID = "2";
            }
            if (chanpin == "油管(N80Q)")
            {
                CID = "3";
            }
            if (chanpin == "套管(N80Q)")
            {
                CID = "4";
            }
            string DID = ((Label)this.GridView1.Rows[selectRow].Cells[7].FindControl("Label10")).Text;
            string DT = ((Label)this.GridView1.Rows[selectRow].Cells[6].FindControl("Label9")).Text;
            Response.Redirect("/Sale/产品装车单.aspx?CID=" + CID + "&&DID=" + DID + "&&DT="+DT);
        }      
       
    }
}
